<?php
session_start();
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct(){
		$this->need_login = false;//控制是否需要登录
		parent::__construct();
	}
	public function index()
	{
		$this->load->view('welcome_message');
	}
	public function check(){
		$data = $this->input->post();
		$this->load->model("User_model");
		$data = $this->User_model->check($data);
		if($data){
			echo "登录成功";
			$_SESSION['data'] = $data['id'];
			$url = "/index.php/goods/index";//判断登录超时后，要跳转到的页面
			echo "<script language='javascript' type='text/javascript'>";
			echo "setTimeout(function(){window.location.href='$url'},2000)";
			echo "</script>";
		}else{
			echo "用户名或密码错误！";
			$url = "/index.php/welcome/index";//判断登录超时后，要跳转到的页面
			echo "<script language='javascript' type='text/javascript'>";
			echo "setTimeout(function(){window.location.href='$url'},2000)";
			echo "</script>";
		}
	}
	public function register(){
		$data = $this->input->post();
		$id['id'] = $this->input->get('id');
		if($data){
			$this->load->model("User_model");
			$result = $this->User_model->add($data);
			if($result){//此处if里面为true
				echo "注册成功";
				$this->load->model("Balance_log_model");
				$this->Balance_log_model->add(array(
					'user_id'=>$result,
					'now_balance'=>200,
					'wave'=>'+200',
					'source'=>'新用户注册',
					'create_time'=>time()
				));
				$url = "/index.php/welcome/index";//判断登录超时后，要跳转到的页面
				echo "<script language='javascript' type='text/javascript'>";
				echo "setTimeout(function(){window.location.href='$url'},2000)";
				echo "</script>";
			}
		}else{
			$this->load->view('register',$id);
		}
	}
	public function shop(){
		//商品信息
		$id = $this->input->get('id');
		$this->load->model("Goods_model");
		$arr_goods = $this->Goods_model->getOne($id);
		//用户信息
		$this->load->model("User_model");
		$arr_user = $this->User_model->getOne($_SESSION['data']);
		if($arr_user['balance']>=$arr_goods['price']){
			$user_res = $this->User_model->update($arr_user['id'],'balance',$arr_user['balance']-$arr_goods['price']);
			$this->load->model("Balance_log_model");
			$this->Balance_log_model->add(array(
				'user_id'=>$arr_user['id'],
				'now_balance'=>$arr_user['balance']-$arr_goods['price'],
				'wave'=>'-'.$arr_goods['price'],
				'source'=>'购买商品'.$arr_goods['name'],
				'create_time'=>time()
			));
			if($user_res){
				if($arr_user['status']==0){
					$this->User_model->update($arr_user['id'],'status',1);
					$this->code($arr_user['id']);
				}
				//一级分销商
				if(!empty($arr_user['invite_id'])){
					$invite_one = $this->User_model->getOne($arr_user['invite_id']);
					$this->User_model->update($arr_user['invite_id'],'balance',$invite_one['balance']+$arr_goods['price']*0.1);
					$this->load->model("Balance_log_model");
					$this->Balance_log_model->add(array(
						'user_id'=>$arr_user['invite_id'],
						'now_balance'=>$invite_one['balance']+$arr_goods['price']*0.1,
						'wave'=>'+'.$arr_goods['price']*0.1,
						'source'=>'用户:'.$arr_user['name'].' 购买商品'.$arr_goods['name'].'获得的分利',
						'create_time'=>time()
					));
				}
				// 第二级分销商
				if(!empty($invite_one['invite_id'])){
					$invite_two = $this->User_model->getOne($invite_one['invite_id']);
					$this->User_model->update($invite_one['invite_id'],'balance',$invite_two['balance']+$arr_goods['price']*0.06);
					$this->load->model("Balance_log_model");
					$this->Balance_log_model->add(array(
						'user_id'=>$invite_one['invite_id'],
						'now_balance'=>$invite_two['balance']+$arr_goods['price']*0.06,
						'wave'=>'+'.$arr_goods['price']*0.06,
						'source'=>'用户:'.$arr_user['name'].' 购买商品'.$arr_goods['name'].'获得的分利',
						'create_time'=>time()
					));
				}
				// 第三级分销商
				if(!empty($invite_two['invite_id'])){
					$invite_three = $this->User_model->getOne($invite_two['invite_id']);
					$this->User_model->update($invite_two['invite_id'],'balance',$invite_three['balance']+$arr_goods['price']*0.03);
					$this->load->model("Balance_log_model");
					$this->Balance_log_model->add(array(
						'user_id'=>$invite_two['invite_id'],
						'now_balance'=>$invite_three['balance']+$arr_goods['price']*0.03,
						'wave'=>'+'.$arr_goods['price']*0.03,
						'source'=>'用户:'.$arr_user['name'].' 购买商品'.$arr_goods['name'].'获得的分利',
						'create_time'=>time()
					));
				}
			}
			echo "购买成功！！";
			$url = "/index.php/goods/index";//要跳转到的页面
			echo "<script language='javascript' type='text/javascript'>";
			echo "setTimeout(function(){window.location.href='$url'},2000)";
			echo "</script>";
		}else{
			echo "您的余额不足！请充值后再进行购买";
			$url = "/index.php/goods/index";//要跳转到的页面
			echo "<script language='javascript' type='text/javascript'>";
			echo "setTimeout(function(){window.location.href='$url'},2000)";
			echo "</script>";
		}
	}
	public function code($id){
		require_once str_replace('\\' , '/' , APPPATH).'libraries/phpqrcode/phpqrcode.php';
		$value='http://'.$_SERVER['SERVER_NAME'].'/index.php/welcome/register?id='.$id;//二维码内容 
		$errorCorrectionLevel='L';//容错级别 
		$matrixPointSize=6;//生成图片大小 
		$path='public/image/';//图片位置
		$name=rand(1,1000).time().'.png';//图片名
		$file=$path.$name;
		//生成二维码图片 
		QRcode::png($value,$file,$errorCorrectionLevel,$matrixPointSize,2);
		$this->User_model->update($id,'qrcode','http://'.$_SERVER['SERVER_NAME'].'/'.$file);
	}
	public function checks(){
		$name = $this->input->post('name');
		$this->load->model("User_model");
		$arr_user = $this->User_model->checkName($name);
		if($arr_user){
			echo  1;
		}else{
			echo  2;
		}
	}
}

